<?php

namespace Portal\Domain;

use Portal\Model\Role as RoleModel;
use Portal\Model\AdminRole as AdminRoleModel;
use PhalApi\Exception\BadRequestException;

class AdminRole
{
    public function insertAdminRole($adminId, $role)
    {
        //判断是否有$role类型，没有删除用户id并返回403
        $roleModel = new RoleModel();
        $roleData = $roleModel->getDataBy('code', $role, 'id', 0);
        if (isset($roleData['id'])) {
            //插入AdminRole数据
            $adminRoleDomain = new \Portal\Domain\AdminRole();
            $roleId = $roleData['id'];
            AdminRoleModel::notorm()->insert(['user_id' => $adminId,'role_id' => $roleId]);
            ///插入AdminRole数据
        } else {
            //没有对应code的角色，删除用户表并返回403
            \Portal\Model\Admin::notorm()->where(['id' => $adminId])->delete();
            throw new BadRequestException('用户角色不存在！', 3);
        }
    }
    public function getRoleIds($userId)
    {
        $datas = AdminRoleModel::notorm()->select('role_id')->where(['user_id' => $userId])->fetchAll();
        $returnData = [];
        foreach ($datas as $data) {
            $returnData[] = $data['role_id'];
        }
        // \PhalApi\DI()->cache->set($cacheKey, $returnData);
        return $returnData;
    }
    //用户数据更新时，更新新的权限
    public function updateRoleIds($userId, $oldRoleIds, $newRoleIds)
    {

        //数字转换为数组
        if (is_numeric($newRoleIds)) {
            $newRoleIds = [$newRoleIds];
        } elseif (is_string($newRoleIds)) {
            //json格式转换为数组
            $newRoleIds = json_decode($newRoleIds, true);
        }
        if (!is_array($newRoleIds) || !is_array($oldRoleIds)) {
            return ;
        }
        //
        //获取新增加的角色id
        $addRoleIds = array_diff($newRoleIds, $oldRoleIds);

        //获取删除掉的角色id
        $delRoleIds = array_diff($oldRoleIds, $newRoleIds);
        $adminRoleModel = new AdminRoleModel();
        //删除处理
        if (!empty($delRoleIds)) {
            AdminRoleModel::notorm()->where(['role_id' => $delRoleIds,'user_id' => $userId])->delete();
        }
        //新增处理
        if (!empty($addRoleIds)) {
            $insertDatas = [];
            foreach ($addRoleIds as $addRoleId) {
                $insertData['user_id'] = $userId;
                $insertData['role_id'] = $addRoleId;
                $insertDatas[] = $insertData;
            }
            $adminRoleModel->insertMore($insertDatas);
        }
    }

    //列表所有超级管理员id
    //列表所有超级管理员id
    public function getRootIds()
    {
        $returnData = [];
        $rootDatas = AdminRoleModel::notorm()->select('user_id')->where(['role_id' => 1])->fetchAll();
        if (is_array($rootDatas) && !empty($rootDatas)) {
            foreach ($rootDatas as $rootData) {
                $returnData[] = $rootData['user_id'];
            }
        }
        return $returnData;
    }

    public function getUserIdsByRoleIds($roleIds)
    {
        $userIdDatas = AdminRoleModel::notorm()->select('user_id')->where(['role_id' => $roleIds])->fetchAll();
        $ids = [];
        foreach ($userIdDatas as $userIdData) {
            $ids[] = $userIdData['user_id'];
        }
        $ids = array_unique($ids);
        return $ids;
    }
}
